package es.onlysolutions.gs.web.controller;

import es.onlysolutions.arq.core.mvc.command.AbstractCommandBean;
import es.onlysolutions.gs.web.commandBean.RedirectBean;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Controller to redirect directly to a JSP. With this strategy the access to JSP are
 * controlled by the GenericController too.
 */
public class RedirectController extends GSController
{
    public static final String REDIRECT_VIEW = "redirectView";

    protected AbstractCommandBean loadPage(HttpServletRequest request) throws Exception
    {
        return null;
    }


    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException bindException) throws Exception
    {
        RedirectBean bean = (RedirectBean) command;

        String redirectView = ServletRequestUtils.getStringParameter( request, REDIRECT_VIEW, "" );

        if( redirectView == null || redirectView.length() == 0 )
        {
            redirectView = bean.getRedirectView();
        }

        return new ModelAndView( redirectView );
    }


    @Override
    protected boolean isFormSubmission(HttpServletRequest request)
    {
        return true;
    }
    
}
